﻿using UnityEngine;
using System.Collections;

public class ScreenScaleManager  {



	public static int WIDTHSCREEN=1920;
	public static int HEIGHTSCREEN=1080;
	
	public static int WIDTHDEVICE=Screen.width;
	public static int HEIGHTDEVICE=Screen.height;
	//public static int WIDTHDEVICE=240;
	//public static int HEIGHTDEVICE=320;
	
	public static float SCALEWIDTH=(float)WIDTHDEVICE/WIDTHSCREEN;
	public static float SCALEHEIGHT=(float)HEIGHTDEVICE/HEIGHTSCREEN;
	
	public static float SCALEMIN=SCALEWIDTH<SCALEHEIGHT?SCALEWIDTH:SCALEHEIGHT;
	public static float SCALEMAX=SCALEWIDTH>SCALEHEIGHT?SCALEWIDTH:SCALEHEIGHT;
	
	public static float WIDTHFIT=WIDTHSCREEN*SCALEMIN;
	public static float HEIGHTFIT=HEIGHTSCREEN*SCALEMIN;
	public static float WIDTHFILL=WIDTHSCREEN*SCALEMAX;
	public static float HEIGHTFILL=HEIGHTSCREEN*SCALEMAX;
	
	public static float DXFIT=(WIDTHDEVICE-WIDTHFIT)/2;
	public static float DYFIT=(HEIGHTDEVICE-HEIGHTFIT)/2;
	public static float DXFILL=(WIDTHDEVICE-WIDTHFILL)/2;
	public static float DYFILL=(HEIGHTDEVICE-HEIGHTFILL)/2;
	
	public static void Init()
	{
		WIDTHDEVICE=Screen.width;
		HEIGHTDEVICE=Screen.height;
		
		SCALEWIDTH=(float)WIDTHDEVICE/WIDTHSCREEN;
		SCALEHEIGHT=(float)HEIGHTDEVICE/HEIGHTSCREEN;
		
	SCALEMIN=SCALEWIDTH<SCALEHEIGHT?SCALEWIDTH:SCALEHEIGHT;
	SCALEMAX=SCALEWIDTH>SCALEHEIGHT?SCALEWIDTH:SCALEHEIGHT;
		
		WIDTHFIT=WIDTHSCREEN*SCALEMIN;
		HEIGHTFIT=HEIGHTSCREEN*SCALEMIN;
		WIDTHFILL=WIDTHSCREEN*SCALEMAX;
		HEIGHTFILL=HEIGHTSCREEN*SCALEMAX;
		
		DXFIT=(WIDTHDEVICE-WIDTHFIT)/2;
		DYFIT=(HEIGHTDEVICE-HEIGHTFIT)/2;
		DXFILL=(WIDTHDEVICE-WIDTHFILL)/2;
		DYFILL=(HEIGHTDEVICE-HEIGHTFILL)/2;
	}
	public static Rect GetFillRect(Rect rect)
	{
		return new Rect(rect.x*SCALEMAX+DXFILL,rect.y*SCALEMAX+DYFILL,rect.width*SCALEMAX,rect.height*SCALEMAX);
	}
	public static Rect GetFitRect(Rect rect,Anchor anchor)
	{
		switch(anchor)
		{
		case Anchor.RightBottom: return new Rect(rect.x*SCALEMIN+DXFIT*2,rect.y*SCALEMIN+DYFIT*2,rect.width*SCALEMIN,rect.height*SCALEMIN);
		case Anchor.LeftBottom: return new Rect(rect.x*SCALEMIN,rect.y*SCALEMIN+DYFIT*2,rect.width*SCALEMIN,rect.height*SCALEMIN);
		case Anchor.LeftTop: return new Rect(rect.x*SCALEMIN,rect.y*SCALEMIN,rect.width*SCALEMIN,rect.height*SCALEMIN);
		case Anchor.RightTop: return new Rect(rect.x*SCALEMIN+DXFIT*2,rect.y*SCALEMIN,rect.width*SCALEMIN,rect.height*SCALEMIN);
		case Anchor.CenterTop: return new Rect(rect.x*SCALEMIN+DXFIT,rect.y*SCALEMIN,rect.width*SCALEMIN,rect.height*SCALEMIN);
		case Anchor.CenterBottom: return new Rect(rect.x*SCALEMIN+DXFIT,rect.y*SCALEMIN+DYFIT*2,rect.width*SCALEMIN,rect.height*SCALEMIN);
		default: return new Rect(rect.x*SCALEMIN+DXFIT,rect.y*SCALEMIN+DYFIT,rect.width*SCALEMIN,rect.height*SCALEMIN);
		}
	}
	public static Vector2 GetFitPoint(Vector2 pos,Anchor anchor)
	{
		switch(anchor)
		{
		case Anchor.RightBottom: return new Vector2(pos.x*SCALEMIN+DXFIT*2,pos.y*SCALEMIN+DYFIT*2);
		default: return new Vector2(pos.x*SCALEMIN+DXFIT,pos.y*SCALEMIN+DYFIT);
		}
	}
	
	public static Rect GetFitRect(Rect rect)
	{
		return new Rect(rect.x*SCALEMIN+DXFIT,rect.y*SCALEMIN+DYFIT,rect.width*SCALEMIN,rect.height*SCALEMIN);
	}
	public static Rect GetFitRectRight(Rect rect)
	{
		return new Rect(rect.x*SCALEMIN+DXFIT*2,rect.y*SCALEMIN+DYFIT,rect.width*SCALEMIN,rect.height*SCALEMIN);
	}
	public static Rect GetFitRectLeftTop(Rect rect)
	{
		return new Rect(rect.x*SCALEMIN,rect.y*SCALEMIN,rect.width*SCALEMIN,rect.height*SCALEMIN);
	}
	
	public static Vector2 GetFitPoint(Vector2 pos)
	{
		return new Vector2(pos.x*SCALEMIN+DXFIT,pos.y*SCALEMIN+DYFIT);
	}
	public static Vector2 GetFitPointRight(Vector2 pos)
	{
		return new Vector2(pos.x*SCALEMIN+DXFIT*2,pos.y*SCALEMIN+DYFIT);
	}
	public static int GetFitValue(int value)
	{
		return (int)(value*SCALEMIN);
	}

}
public enum Anchor{
	LeftTop,
	LeftBottom,
	RightTop,
	RightBottom,
	CenterTop,
	CenterBottom,
	Center
}
